package com.android.qjpmc.data_module;

import com.android.qjpmc.bean.PointParameterBean;

import java.util.ArrayList;
import java.util.List;

public class DataTeam {
    static final byte DATA_TYPE_ANALOG = 0;
    static final byte DATA_TYPE_SIGNAL = 1;
    int m_Type;
    int m_ItemCount = 0;
    int m_ID = 0;
    AnalogDataItem[] m_AnalogItem = null;
    DigitalDataItem[] m_DigitalItem = null;
    ArrayList<AnalogDataItem> m_AnalogList = new ArrayList<>();


    List<PointParameterBean> m_ParameterBeanList = null;

    public List<PointParameterBean> GetParameterBeanList()
    {
        return m_ParameterBeanList;
    }


    ArrayList<AnalogDataItem> GetAnalogValueList() {
        return m_AnalogList;
    }

    DataTeam m_NextNode = null;
    DataTeam m_PreNode = null;

    DataLocation m_DataLocation = new DataLocation();

    public DataTeam GetNext() {
        return m_NextNode;
    }

    public DataTeam GetPre() {
        return m_PreNode;
    }

    public void SetNext(DataTeam Node) {
        m_NextNode = Node;
    }

    public void SetPre(DataTeam Node) {
        m_PreNode = Node;
    }

    public int AppendANode(DataTeam Node) {
        DataTeam NextNode = GetNext();
        if (Node == null) {
            return 1;
        }
        Node.SetNext(NextNode);
        if (NextNode != null) {
            NextNode.SetPre(Node);
        }
        return 0;
    }


    //创建数据对象
    public int CreateTeamData(byte DataType, int DataCount) {
        m_AnalogItem = null;
        m_DigitalItem = null;
        m_Type = DataType;
        int index = 0;
        switch (DataType) {
            case DATA_TYPE_ANALOG: {
                m_AnalogList.clear();
                m_AnalogItem = new AnalogDataItem[DataCount];
                m_ItemCount = DataCount;
                for (index = 0; index < m_ItemCount; index++) {
                    m_AnalogItem[index] = new AnalogDataItem();
                    m_AnalogList.add(m_AnalogItem[index]);
                }
            }
            break;
            case DATA_TYPE_SIGNAL: {
                m_DigitalItem = new DigitalDataItem[DataCount];
                m_ItemCount = DataCount;
                for (index = 0; index < m_ItemCount; index++) {
                    m_DigitalItem[index] = new DigitalDataItem();
                }
            }
            break;
            default: {

            }
        }
        return 0;
    }

    //析构函数
    public void finalize() {
        m_AnalogItem = null;
        m_DigitalItem = null;
    }

    public int IsMe(int ID) {
        if (m_ID == ID) {
            return 1;
        }
        return 0;
    }

    public int SetName(int DataIndex, String Name) {
        if (m_AnalogItem == null) {
            return 1;
        }
        if (DataIndex >= m_ItemCount) {
            return 1;
        }
        m_AnalogItem[DataIndex].m_Name = Name;
        return 0;
    }

    public int SetUnit(int DataIndex, String Unit) {
        if (m_AnalogItem == null) {
            return 1;
        }
        if (DataIndex >= m_ItemCount) {
            return 1;
        }
        m_AnalogItem[DataIndex].m_Unit = Unit;
        return 0;
    }
    //获取Bean List
    public List<PointParameterBean> GetValueBeanList() {
        List<PointParameterBean> beanList = new ArrayList<PointParameterBean>();
        int index = 0;
        switch (m_Type) {
            case DATA_TYPE_ANALOG: {
                for (index = 0; index < m_ItemCount; index++) {
                    beanList.add(m_AnalogItem[index].GetBean());
                }
            }
            break;
            case DATA_TYPE_SIGNAL: {

            }
            break;
        }
        return beanList;
    }
    public int CreateAnalogTeam(List<PointParameterBean> pointList)
    {
        ArrayList<AnalogDataItem> list = GetAnalogValueList();
        AnalogDataItem item = null;
        if (list == null)
        {
            return 1;
        }
        list.clear();
        pointList.size();
        for(PointParameterBean point: pointList)
        {
            item = new AnalogDataItem();
            if (item == null)
            {
                return 1;
            }
            item.m_Name = point.propsName;

            if (item.m_Name == null)
            {
                item.m_Name = "---";
            }
            item.m_TagName = point.tagnode;

            int ChangeUnitName = 0;
            if (point.analogUnit != null)
            {
                if (point.analogUnit.equals("NULL"))
                {
                    ChangeUnitName = 1;
                } else {
                    item.m_Unit = point.analogUnit;
                }
            }
            else
            {
                ChangeUnitName = 1;
            }

            if (ChangeUnitName == 1)
            {
                if (item.m_Name.equals("Uab") || item.m_Name.equals("Ubc") || item.m_Name.equals("Uca") ) {
                    item.m_Unit = "V";
                } else if (item.m_Name.equals("Ia") || item.m_Name.equals("Ib") || item.m_Name.equals("Ic") ) {
                    item.m_Unit = "A";
                } else if (item.m_Name.equals("P") ) {
                    item.m_Unit = "w";
                } else if (item.m_Name.equals("Q") ) {
                    item.m_Unit = "Var";
                } else {
                    item.m_Unit = "";
                }
            }

            point.analogUnit = item.m_Unit;

            item.m_IDInGroup = point.groupIndex;
            item.SetBean(point);
            list.add(item);
        }
        m_ParameterBeanList = pointList;
        return 0;
    }

    //更新实时数据
    public int UpdateAnalogTeam(List<PointParameterBean> pointList)
    {
        ArrayList<AnalogDataItem> list = GetAnalogValueList();
        AnalogDataItem item = null;
        if (list == null)
        {
            return 1;
        }
        //list.clear();
        int pointsize = pointList.size();
        int valueIdx = 0;
        for(PointParameterBean point: pointList)
        {
            if (valueIdx >= list.size() )
            {

            }
            else
            {
                item = list.get(valueIdx);// new AnalogDataItem();
                if (item == null)
                {
                    ;
                }
                else {
                    item.m_Unit = point.analogUnit;
                    item.m_Name = point.propsName;
                    if (point.pointData != null) {
                        if (point.pointData.value != null) {
                            item.m_value = Float.parseFloat(point.pointData.value);
                        }
                    }
                    item.SetBean(point);
                }
            }

            valueIdx++;

            //list.add(item);
        }
        return 0;
    }
}
